/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import pkl51.error.EntityException;

/**
 *
 * @author Hindarwan
 */
public class ListBlok6BModel implements Serializable{

    private static ListBlok6BModel instance;
    private static final long serialVersionUID = -2800046825933178058L;
    private List<Blok6bRowModel> listAllBlok6B;

    public static ListBlok6BModel getInstance() {
        if (instance == null) {
            instance = new ListBlok6BModel();
        }
        return instance;
    }

    private ListBlok6BModel() {
        listAllBlok6B = new ArrayList<Blok6bRowModel>();
    }

    public void addToSuperList(List<Blok6bRowModel> rowModels) {
        for (Blok6bRowModel blok6bRowModel : rowModels) {
            listAllBlok6B.add(blok6bRowModel);
        }
    }

    public void insertAllBlok6B() throws EntityException {
        int id = 0;
        for (Blok6bRowModel blok6bRowModel : listAllBlok6B) {
            ++id;
            if (isEmpty(blok6bRowModel)) {
                continue;
            }
            try {
                blok6bRowModel.insertBlok6bRow(id);
            } catch (NullPointerException npe) {
            }
        }
    }

    public void updateAllBlok6B() throws EntityException {
        int id = 0;
        for (Blok6bRowModel blok6bRowModel : listAllBlok6B) {
            ++id;
            if (isEmpty(blok6bRowModel)) {
                continue;
            }
            try {
                blok6bRowModel.updateBlok6bRow(id);
            } catch (NullPointerException npe) {
            }
        }
    }

    public void getBlokAll6b() throws EntityException {
        int id = 0;
        for (Blok6bRowModel blok6bRowModel : listAllBlok6B) {
            try {
                blok6bRowModel.getBlok6bRow(++id);
            } catch (NullPointerException ex) {
            }
        }
    }

    public boolean isEmpty(Blok6bRowModel model) {
        if (model.getB6BC1().isEmpty() && model.getB6BC3().isEmpty() && model.getB6BC5().isEmpty()) {
            return true;
        } else {
            if (model.getB6BC1().isEmpty()) {
                model.setB6BC1("0");
            }
            if (model.getB6BC2().isEmpty()) {
                model.setB6BC2("0");
            }
            if (model.getB6BC3().isEmpty()) {
                model.setB6BC3("0");
            }
            if (model.getB6BC4().isEmpty()) {
                model.setB6BC4("0");
            }
            if (model.getB6BC5().isEmpty()) {
                model.setB6BC5("0");
            }
            return false;
        }
    }
    
    public void resetModel(){
        for (Blok6bRowModel blok6bRowModel : listAllBlok6B) {
            blok6bRowModel.resetModel();
        }
    }
}
